package com.itstyle.quartz.interceptor;


import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.google.gson.Gson;
import com.itstyle.quartz.common.IPUtils;
import com.itstyle.quartz.entity.BaseLogEntity;


@Component
@Aspect
public class LogAspect
{
    private final static Logger logger = LoggerFactory.getLogger(LogAspect.class);


    private BaseLogEntity baseLogEntity = new BaseLogEntity();
    /**
     * @Title：doBeforeInServiceLayer
     * @Description: 方法调用前触发 记录开始时间
     * @author 
     * @date 
     * @param joinPoint
     */
    @Before("execution(* com.itstyle.quartz.web..*.*(..))")
    public void doBeforeInServiceLayer(JoinPoint joinPoint)
    {
        baseLogEntity.setStartTimeMillis(
            new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis()));
    }

    /**
     * @Title：doAfterInServiceLayer
     * @Description: 方法调用后触发 记录结束时间
     * @author 
     * @date 
     * @param joinPoint
     */
    @After("execution(* com.itstyle.quartz.web..*.*(..))")
    public void doAfterInServiceLayer(JoinPoint joinPoint)
    {
        // endTimeMillis = System.currentTimeMillis(); // 记录方法执行完成的时间

        baseLogEntity.setEndTimeMillis(
            new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis()));
        this.printOptLog();
    }

    /**
     * @Title：doAround
     * @Description: 环绕触发
     * @author 
     * @date 
     * @param pjp
     * @return
     * @throws Throwable
     */
    @Around("execution(* com.itstyle.quartz.web..*.*(..))")
    public Object doAround(ProceedingJoinPoint pjp)
        throws Throwable
    {
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes)ra;
        HttpServletRequest request = sra.getRequest();
        Object result = pjp.proceed();// result的值就是被拦截方法的返回值
        baseLogEntity.setIp(IPUtils.getClientAddress(request));
        baseLogEntity.setMethod(request.getMethod());
        baseLogEntity.setParam(request.getParameterMap());
        baseLogEntity.setReslut(result);
        baseLogEntity.setUrl(request.getRequestURL().toString());
        baseLogEntity.setUri(request.getRequestURI());
        return result;
    }

    /**
     * @Title：printOptLog
     * @Description: 输出日志
     * @author shaojian.yu
     * @date 2014年11月2日 下午4:47:09
     */
    private void printOptLog()
    {
        Gson gson = new Gson(); // 需要用到google的gson解析包
        logger.info(gson.toJson(baseLogEntity));

    }
}
